package com.gs.SynAntiWandering.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;

import com.gs.SynAntiWandering.R;
import com.gs.SynAntiWandering.activity.base.BaseFragmentActivity;
import com.gs.SynAntiWandering.constant.AllConstants;
import com.gs.SynAntiWandering.control.ToolBarTitle;
import com.gs.SynAntiWandering.util.SPUtil;


public class CameraAngleActivity extends BaseFragmentActivity implements View.OnClickListener {

    private RadioButton faceUp, faceRight, faceDown, faceLeft;
    private final int zero = 0;
    private final int ninety = 90;
    private final int oneHundredEighty = 180;
    private final int twoHundredSeventy = 270;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_camera_angle;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        faceUp = fd(R.id.rb_face_up);
        faceRight = fd(R.id.rb_face_right);
        faceDown = fd(R.id.rb_face_down);
        faceLeft = fd(R.id.rb_face_left);
        ToolBarTitle toolBarTitle = fd(R.id.toolBarTitle);
        toolBarTitle.setOnClickListener(this);
        faceUp.setOnClickListener(this);
        faceRight.setOnClickListener(this);
        faceDown.setOnClickListener(this);
        faceLeft.setOnClickListener(this);
        fd(R.id.btn_ok).setOnClickListener(this);
        int angle = SPUtil.getInstance().getInt(AllConstants.FACE_ANGLE, zero);
        if (angle == zero) {
            faceUp.setChecked(true);
        } else if (angle == ninety) {
            faceRight.setChecked(true);
        } else if (angle == oneHundredEighty) {
            faceDown.setChecked(true);
        } else if (angle == twoHundredSeventy) {
            faceLeft.setChecked(true);
        }
    }

    @Override
    protected void initFragment() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.item_back) {
            finish();
        } else if (id == R.id.rb_face_up) {
            faceRight.setChecked(false);
            faceDown.setChecked(false);
            faceLeft.setChecked(false);
        } else if (id == R.id.rb_face_right) {
            faceUp.setChecked(false);
            faceDown.setChecked(false);
            faceLeft.setChecked(false);
        } else if (id == R.id.rb_face_down) {
            faceUp.setChecked(false);
            faceRight.setChecked(false);
            faceLeft.setChecked(false);
        } else if (id == R.id.rb_face_left) {
            faceUp.setChecked(false);
            faceRight.setChecked(false);
            faceDown.setChecked(false);
        } else if (id == R.id.btn_ok) {
            if (faceUp.isChecked()) {
                SPUtil.getInstance().putInt(AllConstants.FACE_ANGLE, zero);
            }
            if (faceRight.isChecked()) {
                SPUtil.getInstance().putInt(AllConstants.FACE_ANGLE, ninety);
            }
            if (faceDown.isChecked()) {
                SPUtil.getInstance().putInt(AllConstants.FACE_ANGLE, oneHundredEighty);
            }
            if (faceLeft.isChecked()) {
                SPUtil.getInstance().putInt(AllConstants.FACE_ANGLE, twoHundredSeventy);
            }
            setResult(1);
            finish();
        }
    }
}